package com.asm.DBloader;

import java.util.ArrayList;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;

public class ASM_FAvDetDS {
	private SQLiteDatabase dB;
	private ASM_DB dbHelper;
	Context context;

	
	public ASM_FAvDetDS (Context context)
	{
		this.context = context;
		dbHelper = new ASM_DB(context);
	}
	
	public void open() throws SQLException
	{
		dB = dbHelper.getWritableDatabase();
	}
	
	
	public int Create(ASM_FAvDet list2) {
		
		
		int serverdbID =0;
		
		if(dB == null){
			open();
		}else if(!dB.isOpen())
		{
			open();
		}
		Cursor cursor = null;
		//for (ASM_FAvDet det : list2) {
			
		String selectQuery = "SELECT * FROM fAvDet WHERE _id = '"+list2.getASM_FAVDET_ID()+"'";
		cursor = dB.rawQuery(selectQuery, null);
		
		ContentValues values = new ContentValues();
		values.put(ASM_DB.ASM_FAVDET_REFNO,list2.getASM_FAVDET_REFNO());
		values.put(ASM_DB.ASM_FAVDET_TXNDATE,list2.getASM_FAVDET_TXNDATE());

		values.put(ASM_DB.ASM_FAVDET_ITEMCODE,list2.getASM_FAVDET_ITEMCODE());
		values.put(ASM_DB.ASM_FAVDET_ITEMNAME,list2.getASM_FAVDET_ITEMNAME());
		values.put(ASM_DB.ASM_FAVDET_COMPSTATUS,list2.getASM_FAVDET_COMPSTATUS());
		values.put(ASM_DB.ASM_FAVDET_COMPPROD,list2.getASM_FAVDET_COMPPROD());
		values.put(ASM_DB.ASM_FAVDET_UITEMS,list2.getASM_FAVDET_UITEMS());
		values.put(ASM_DB.ASM_FAVDET_USTATUS,list2.getASM_FAVDET_USTATUS());
		values.put(ASM_DB.ASM_FAVDET_HITEMS,list2.getASM_FAVDET_HITEMS());
		
		values.put(ASM_DB.ASM_FAVDET_HSTATUS,list2.getASM_FAVDET_HSTATUS());
		values.put(ASM_DB.ASM_FAVDET_VITEMS,list2.getASM_FAVDET_VITEMS());
		values.put(ASM_DB.ASM_FAVDET_VSTATUS,list2.getASM_FAVDET_VSTATUS());
		values.put(ASM_DB.ASM_FAVDET_ISSYNCED,list2.getASM_FAVDET_ISSYNCED());
		values.put(ASM_DB.ASM_FAVDET_ACTIVESTATE,list2.getASM_FAVDET_ACTIVESTATE());
		int count = cursor.getCount();
		if (count>0) {
			serverdbID = (int) dB.update(ASM_DB.ASM_FAVDET, values, "_id = '"+list2.getASM_FAVDET_ID()+"'", null);
		}else {
			serverdbID = (int) dB.insert(ASM_DB.ASM_FAVDET, null, values);
		}		
																
	
		//count = (int) dB.insert(ASM_DB.ASM_FAVDET, null, values);
		//}
		dB.close();
		return serverdbID;
		
	}
	
	public int ClearFilld(ASM_FAvDet det) 
	{
		
		int count =0;
		
		if(dB == null)
		{
			open();
		}else if(!dB.isOpen())
		{
			open();
		}

		ContentValues values = new ContentValues();
		
		values.put(ASM_DB.ASM_FAVDET_REFNO,det.getASM_FAVDET_REFNO());
		values.put(ASM_DB.ASM_FAVDET_TXNDATE,det.getASM_FAVDET_TXNDATE());

		values.put(ASM_DB.ASM_FAVDET_ITEMCODE,det.getASM_FAVDET_ITEMCODE());
		values.put(ASM_DB.ASM_FAVDET_ITEMNAME,det.getASM_FAVDET_ITEMNAME());
		values.put(ASM_DB.ASM_FAVDET_COMPSTATUS,det.getASM_FAVDET_COMPSTATUS());
		values.put(ASM_DB.ASM_FAVDET_COMPPROD,det.getASM_FAVDET_COMPPROD());
		values.put(ASM_DB.ASM_FAVDET_UITEMS,det.getASM_FAVDET_UITEMS());
		values.put(ASM_DB.ASM_FAVDET_USTATUS,det.getASM_FAVDET_USTATUS());
		values.put(ASM_DB.ASM_FAVDET_HITEMS,det.getASM_FAVDET_HITEMS());
		
		values.put(ASM_DB.ASM_FAVDET_HSTATUS,det.getASM_FAVDET_HSTATUS());
		values.put(ASM_DB.ASM_FAVDET_VITEMS,det.getASM_FAVDET_VITEMS());
		values.put(ASM_DB.ASM_FAVDET_VSTATUS,det.getASM_FAVDET_VSTATUS());
		

		count = (int) dB.update(ASM_DB.ASM_FAVDET, values,"RefNo = '"+det.getASM_FAVDET_ITEMCODE().trim()+"'", null);

		dB.close();
		return count;
		
	}
	public ArrayList<ASM_FAvDet> getfAVDet(String refNo) 
	{
		if (dB == null) 
		{
			open();
		} else if (!dB.isOpen()) 
		{
			open();
		}
		ArrayList<ASM_FAvDet> list = new ArrayList<ASM_FAvDet>();
		
	
		String selectQuery = "select RefNo,Itemcode,Itemname,Cmpstatus,cmpprod,Uitems,Ustatus,Hitems,Hstaus,Vitems,Vstatus from fAvDet where RefNo = '"+refNo+"'";
		
		Cursor cursor = null;
		cursor = dB.rawQuery(selectQuery, null);
		
		//[AVAA/0000017-:--:-A-:-Ryiio-:-5678-:-D-:-3556-:-N-:-566-:-A]
		
		while(cursor.moveToNext())
		{
			ASM_FAvDet AVDet=new ASM_FAvDet();
			
			AVDet.setASM_FAVDET_REFNO(cursor.getString(cursor.getColumnIndex(ASM_DB.ASM_FAVDET_REFNO)));
			AVDet.setASM_FAVDET_ITEMCODE(cursor.getString(cursor.getColumnIndex(ASM_DB.ASM_FAVDET_ITEMCODE)));
			AVDet.setASM_FAVDET_ITEMNAME(cursor.getString(cursor.getColumnIndex(ASM_DB.ASM_FAVDET_ITEMNAME)));
			AVDet.setASM_FAVDET_COMPSTATUS(cursor.getString(cursor.getColumnIndex(ASM_DB.ASM_FAVDET_COMPSTATUS)));
			AVDet.setASM_FAVDET_COMPPROD(cursor.getString(cursor.getColumnIndex(ASM_DB.ASM_FAVDET_COMPPROD)));
			AVDet.setASM_FAVDET_UITEMS(cursor.getString(cursor.getColumnIndex(ASM_DB.ASM_FAVDET_UITEMS)));
			AVDet.setASM_FAVDET_USTATUS(cursor.getString(cursor.getColumnIndex(ASM_DB.ASM_FAVDET_USTATUS)));
			AVDet.setASM_FAVDET_HITEMS(cursor.getString(cursor.getColumnIndex(ASM_DB.ASM_FAVDET_HITEMS)));
			AVDet.setASM_FAVDET_HSTATUS(cursor.getString(cursor.getColumnIndex(ASM_DB.ASM_FAVDET_HSTATUS)));
			AVDet.setASM_FAVDET_VITEMS(cursor.getString(cursor.getColumnIndex(ASM_DB.ASM_FAVDET_VITEMS)));
			AVDet.setASM_FAVDET_VSTATUS(cursor.getString(cursor.getColumnIndex(ASM_DB.ASM_FAVDET_VSTATUS)));
			
			list.add(AVDet);

		
			
			
		}
		
		return list;
	}
	public int Delete(String Refno)
	{
		
		int count =0;
		
		if(dB == null){
			open();
		}else if(!dB.isOpen()){
			open();
		}

		count = (int) dB.delete(ASM_DB.ASM_FAVDET, "RefNo = '"+Refno+"'", null);

		dB.close();
		return count;
		
	}
	
	public String getdetail(String itemname) 
	{
		if (dB == null) {
			open();
		} else if (!dB.isOpen()) {
			open();
		}
		
	
		String details = null;
		String selectQuery = "select status,newsales,uitems,ustatus,hitems,hstatus,vitems,vstatus from fAvtemp where itemname = '"+itemname+"'";
		
		Cursor cursor = null;
		cursor = dB.rawQuery(selectQuery, null);
		
		while(cursor.moveToNext())
		{
		
			details = cursor.getString(0)
					+"-:-"+cursor.getString(1)
					+"-:-"+cursor.getString(2)
					+"-:-"+cursor.getString(3)
					+"-:-"+cursor.getString(4)
					+"-:-"+cursor.getString(5)
					+"-:-"+cursor.getString(6)
					+"-:-"+cursor.getString(7);
		
		
		}
		
		return details;
	}
	public String getSaveDetails(String itemname) 
	{
		if (dB == null) {
			open();
		} else if (!dB.isOpen()) {
			open();
		}
		
	
		String details = null;
		String selectQuery = "select RefNo,Itemcode,Itemname,Cmpstatus,cmpprod,Uitems,Ustatus,Hitems,Hstaus,Vitems,Vstatus from fAvDet where Itemcode = '"+itemname+"'";
		
		Cursor cursor = null;
		cursor = dB.rawQuery(selectQuery, null);
		
		while(cursor.moveToNext())
		{
		
			details = cursor.getString(0)
					+"-:-"+cursor.getString(1)
					+"-:-"+cursor.getString(2)
					+"-:-"+cursor.getString(3)
					+"-:-"+cursor.getString(4)
					+"-:-"+cursor.getString(5)
					+"-:-"+cursor.getString(6)
					+"-:-"+cursor.getString(7)
					+"-:-"+cursor.getString(8)
					+"-:-"+cursor.getString(9)
					+"-:-"+cursor.getString(10);
		
		
		}
		
		return details;
	}
	
	
	public int DeleteOneRow(String RecNo,String RefNo)
	{
		
		int count =0;
		
		if(dB == null){
			open();
		}else if(!dB.isOpen()){
			open();
		}

		count = (int) dB.delete(ASM_DB.ASM_FAVDET, "itemcode = '"+RecNo+"' and RefNo = '"+RefNo+"'", null);

		dB.close();
		return count;
		
	}
	public ArrayList<String> getDetList(String itemcode,String RefNo) 
	{
		if (dB == null) 
		{
			open();
		} else if (!dB.isOpen()) 
		{
			open();
		}
		ArrayList<String> Details = new ArrayList<String>();
		
	
		String selectQuery ="select RefNo,Itemcode,Itemname,Cmpstatus,cmpprod,Uitems,Ustatus,Hitems,Hstaus,Vitems,Vstatus from fAvDet where Itemcode = '"+itemcode+"' and RefNo = '"+RefNo+"'";
		
		Cursor cursor = null;
		cursor = dB.rawQuery(selectQuery, null);
		
		while(cursor.moveToNext())
		{
		

			Details.add(cursor.getString(1));
		
			
			
		}
		
		return Details;
	}
	
}
